import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		username: "",
		// 存储token
		token: localStorage.getItem('token') ? localStorage.getItem('token') : ''
	},
	mutations: {
		// 修改token，并将token存入localStorage
		signin(state,user) {
			state.token = user.phone;
			localStorage.setItem('token', user.phone);
			state.username = user.username;
			state.hasLogin = true;
			localStorage.setItem("hasLogin",state.hasLogin);
		},
		logoff(state) {
			localStorage.removeItem("token");
			localStorage.removeItem("hasLogin");
			state.username = "";
			state.hasLogin = false;
		}
	}
})

export default store
